import {Tools,Toast} from 'weaver-mobile';
import tinycolor from 'tinycolor2';
const getLabel = Tools.getLabel;

// 协作 公共工具类
const CoworkConstant = {
	
	// api请求错误统一处理方法
  catchApiError: (result) => {
    return new Promise((resolve, reject) => {
      if (!result.api_status) {
        Toast.fail(result.msg || getLabel(30651, '操作失败'));
        reject(result);
        return;
      }
      resolve(result);
    });
  },
}

export default CoworkConstant;

/* 统一增加参数 */
export const PARAMS = {
  // _ec_device: 'mobile_ec',
  _ec_ismobile: 'true',
}

export function getFontColorFromBgHex(hex) {
  const color = tinycolor(hex).getBrightness();
  return color < 127 ? '#fff' : '#333';
}

/**
 * 把URL参数解析成对象
 * @param {*} url 
 */
export function parseQueryString(url) {
  var obj = {};
  var keyvalue = [];
  var key = "",
      value = "";
  let stringReg = /[^?&]+=[^?&]+/g;
  var paraString = url.match(stringReg);
  for (let i = 0; i < paraString.length; i++) {
    keyvalue = paraString[i].split("=");
    key = keyvalue[0];
    value = keyvalue[1];
    obj[key] = value;
  }
  return obj;
}

export const compareParams = (a, b) => {
  let aProps = Object.getOwnPropertyNames(a);
  let bProps = Object.getOwnPropertyNames(b);
  let flag = true;
  if (aProps.length !== bProps.length) return false;
  for (let i in a) {
    if (a[i] !== b[i]) {
      if (typeof(a[i]) === 'object') {
        if (!compareParams(a[i], b[i])) {
          flag = false;
          break;
        }
      } else {
        flag = false;
        break;
      }
    }
  }

  return flag;
}

//判断是否是否支持EM接口
const judgeApiSupport = (apiname) => {
  return !!window.em;
}

export const TitleUtil = {
  // 控制EM客户端顶部头信息
  setTitle: (title) => {
    document.title = title ? title : "" ;//和飞信中走此段,和飞信不进下面if语句
    window.document.title = title ? title : getLabel('17855','协作');
    if (!judgeApiSupport("configNavigation")){
      return;
    }
    const nav = {
      'success': function (argument) {
          console.log('controlEmTopNav success', JSON.stringify(argument));
      },
      'fail': function (argument) {
          console.log('controlEmTopNav fail', JSON.stringify(argument));
      },
      'left': {},
      'right': {},
      'title': '',
      showCloseButton: true
    };
   
    window.em.ready(function(){
      if(title===undefined){
        title = "" || getLabel('17855','协作');
      }
      window.em.configNavigation({...nav, title });//微信中走此段
    });
  },
  //重置EM表单的头
  resetEmElement: () => {
    if (judgeApiSupport("resetNavigation")) {
      window.em.ready(function(){
        window.em.resetNavigation({title: ''});
      });
    }
  }
};

export function openHrmCard(id) {
  if (window.em && (typeof window.em.checkJsApi === 'function') && window.em.checkJsApi('openLink')) {
    window.em.openLink({
      url: `/spa/hrm/static4mobile/index.html#/resourceInfo/${id}`,
      openType: 2,
      sysId: window.localStorage.emobile_ec_id,
    });
  }

 // window.openLink(`/spa/hrm/static4mobile/index.html#/resourceInfo/${id}`);
};